package com.lk.service;



import com.lk.entity.Question;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Map;

/**
 * 知识库service
 *
 * @author 薄一鸣
 * @date 2024/7/23 17:41
 * @version V1.0
 */
@Service
public class QuestionService {

    @Autowired
    RestHighLevelClient client;

    public static final String QUESTION_INDEX = "question";

    private int autoIncrNum = 1;

    /**
     * 获取自增编号
     * @return
     */
    public int getAutoIncrNum(){
        return autoIncrNum++;
    }
    /**
     * 获取当前最大 编号
     * @return
     */
    @PostConstruct
    public void init(){
        try{
            autoIncrNum = getLastQuestionNum();
        }catch (Exception e){

        }
    }

    /**
     * 获取最后插入数据的编号 question_num
     *
     * @return
     */

    public int getLastQuestionNum(){

        int num = 1;

        SearchRequest searchRequest = new SearchRequest(QUESTION_INDEX);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.sort(SortBuilders.fieldSort("_id").order(SortOrder.DESC));
        searchSourceBuilder.size(1); // 获取最新的一条文档
        searchRequest.source(searchSourceBuilder);

        try {
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] searchHits = searchResponse.getHits().getHits();
            if (searchHits.length > 0) {
                SearchHit latestHit = searchHits[0];
                Map<String, Object> sourceAsMap = latestHit.getSourceAsMap();
                Question que = mapToQuestion(sourceAsMap);
                if(!"".equals(que.getQuestion_number())){
                    // 当前编号加 1
                    num = Integer.parseInt(que.getQuestion_number()) + 1;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return num;
    }

    /**
     * map 转实体
     * @param map
     * @return
     */
    public Question mapToQuestion(Map<String,Object> map){
        Question question = new Question();
        question.setId(map.getOrDefault("id","").toString());
        question.setVersion(map.getOrDefault("version","").toString());
        question.setTitle(map.getOrDefault("title","").toString());
        question.setContent(map.getOrDefault("content","").toString());
        question.setContent_txt(map.getOrDefault("content_txt","").toString());
        question.setCreate_time(map.getOrDefault("create_time","").toString());
        question.setCreator(map.getOrDefault("creator","").toString());
        question.setQuestion_number(map.getOrDefault("question_number","").toString());
        question.setDiagnosis(map.getOrDefault("diagnosis","").toString());
        question.setDiagnosis_txt(map.getOrDefault("diagnosis_txt","").toString());
        question.setDesc(map.getOrDefault("desc","").toString());
        question.setDesc_txt(map.getOrDefault("desc_txt","").toString());
        question.setProduct(map.getOrDefault("product","").toString());

        return question;
    }

}
